#include "StdAfx.h"
#include "HPBarTheme.h"
#include "StatusBarHPBarPane.h"

CHPBarTheme::CHPBarTheme(CXTPPaintManager* pPaintManager)
	: CXTPProgressPaintManager(pPaintManager)
{
}

CHPBarTheme::~CHPBarTheme()
{
}

void CHPBarTheme::DrawProgress(CDC* pDC, CXTPProgressBase* pProgressBase)
{
	CRect rc(pProgressBase->GetProgressRect());

	XTPPaintTheme theme = GetPaintManager()->BaseTheme();

	int nLower, nUpper, nPos;
	pProgressBase->GetRange(nLower, nUpper);
	nPos = pProgressBase->GetPos();

	int nWidth, dxBlock;

	CRect rcClient(rc);

	rcClient.DeflateRect(1, 1);

	nWidth = rcClient.right - rcClient.left;
	dxBlock = (rcClient.bottom - rcClient.top) * 2 / 3;

	int x = MulDiv(nWidth, nPos - nLower, nUpper - nLower);

	CStatusBarHPBarPane* pProgressBar = dynamic_cast<CStatusBarHPBarPane*>(pProgressBase);

	pDC->FillSolidRect(rc, pProgressBar->GetBackgroundColor());
	pDC->Draw3dRect(rc, GetXtremeColor(COLOR_3DSHADOW), GetXtremeColor(COLOR_3DSHADOW));

	if (rcClient.Width() < 2)
		return;

	COLORREF clrBlock = pProgressBar->GetBarColor();
	DWORD dwHSLBackground = CXTPDrawHelpers::RGBtoHSL(clrBlock);
	DWORD dwL = GetBValue(dwHSLBackground);
	DWORD dwLight = (dwL + 240) / 2;

	COLORREF clrLight = CXTPDrawHelpers::HSLtoRGB(RGB(GetRValue(dwHSLBackground), GetGValue(dwHSLBackground), dwLight));
	DWORD dwDark = MulDiv(dwL, 2, 3);
	COLORREF clrDark = CXTPDrawHelpers::HSLtoRGB(RGB(GetRValue(dwHSLBackground), GetGValue(dwHSLBackground), dwDark));

	if (pProgressBar->IsDraw3D())
	{
		CRect rcBlock;
		rcBlock.SetRect(rcClient.left, rcClient.top, rcClient.left + x, rcClient.bottom);
		XTPDrawHelpers()->GradientFill(pDC, rcBlock, clrLight, clrDark, FALSE);
	}
	else
	{
		pDC->FillSolidRect(rcClient.left, rcClient.top, x, rcClient.Height(), pProgressBar->GetBarColor());
	}

	pDC->SetTextColor(pProgressBar->GetTextColor());
	pDC->SetBkMode(TRANSPARENT);	
	CXTPFontDC font(pDC, pProgressBar->GetTextFont());
	pDC->DrawText(pProgressBar->GetText(), rc, DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX | DT_VCENTER | DT_CENTER);
}